<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
	All fees function from creating updating to delete
*/
class Student_fees Extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->config->load('acl_config');
		$this->page_auth->secure_page($this->config->item('auth_link_to_CRUD_fees'));
		$this->load->model(array('M_fees','M_level_fees'));
		$this->load->helper('misc');
		$this->load->library('token');
		$this->_init_pagnate_config();// initialize pagination style
		$this->view_data['system_message'] = $this->_msg();
		$this->token->set_token();
	}
	
	/*
		Other Fees
	*/
	public function others()
	{
		$this->load->helper('text');
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		/* pagenation */
		
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'student_fees/others';
		$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('other');
		$this->pagnate_config['per_page'] = 20;
		$this->pagination->initialize($this->pagnate_config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['fee_data'] = $this->M_fees->fetch_fees('other',$this->pagnate_config["per_page"], $page);
		$this->view_data['links'] = $this->pagination->create_links();
	}
	
	/*
		Elective Fees
	*/
	
	public function electives()
	{
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		/* pagenation */
		
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'student_fees/electives/';
		$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('elec');
		$this->pagnate_config['per_page'] = 10;
		$this->pagination->initialize($this->pagnate_config);
			
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['fee_data'] = $this->M_fees->fetch_fees('elec',$this->pagnate_config["per_page"], $page);
		$this->view_data['links'] = $this->pagination->create_links();
	}
	
	function miscellaneous($x = FALSE)
	{
		$this->load->helper('text');
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		/* pagenation */
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'student_fees/miscellaneous/';
		$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('misc');
		$this->pagnate_config['per_page'] = 30;
		$this->pagination->initialize($this->pagnate_config);
			
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['fee_data'] = $this->M_fees->fetch_fees('misc',$this->pagnate_config["per_page"], $page);
		$this->view_data['links'] = $this->pagination->create_links();
	}
	
	
	/* 
	function uniform()
	{
		$this->load->helper('text');
		$this->view_data['system_message'] = $this->session->flashdata('system_message');

		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'student_fees/uniform/';
		$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('uniform');
		$this->pagnate_config['per_page'] = 10;
		$this->pagination->initialize($this->pagnate_config);
			
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['fee_data'] = $this->M_fees->fetch_fees('uniform',$this->pagnate_config["per_page"], $page);
		$this->view_data['links'] = $this->pagination->create_links();
	}
	*/
	
	public function cca()
	{
		$this->load->helper('text');
		$this->view_data['system_message'] = $this->session->flashdata('system_message');

		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'student_fees/cca/';
		$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('cca');
		$this->pagnate_config['per_page'] = 10;
		$this->pagination->initialize($this->pagnate_config);
			
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->view_data['fee_data'] = $this->M_fees->fetch_fees('cca',$this->pagnate_config["per_page"], $page);
		$this->view_data['links'] = $this->pagination->create_links();	
	}
	
	function tuition()
	{
		$this->load->helper('text');
		$this->load->model('M_levels','l');
		$this->view_data['levels'] = $this->l->get_all_levels_array();
		
		if($this->input->get('i'))
		{
			$id = intval($this->input->get('i'));
			$this->view_data['fee_data'] = $this->M_fees->get_tuition_fees_by_grade_level($id);
			$this->view_data["links"] = NULL;
		}else{
			$this->load->helper('text');
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			/* pagenation */
			$this->load->library('pagination');
			$this->pagnate_config['base_url'] = base_url().'student_fees/tuition';
			$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('tuit');
			$this->pagnate_config['per_page'] = 10;
			$this->pagination->initialize($this->pagnate_config);
			
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$this->view_data['fee_data'] = $this->M_fees->fetch_fees('tuit',$this->pagnate_config["per_page"], $page);
			$this->view_data["links"] = $this->pagination->create_links();
		}
	}
	
	function assessment()
	{
		$this->load->helper('text');
		$this->load->model('M_levels','l');
		$this->view_data['levels'] = $this->l->get_all_levels_array();
		
		if($this->input->get('i'))
		{
			$id = intval($this->input->get('i'));
			$this->view_data['fee_data'] = $this->M_fees->get_assessment_fees_by_grade_level($id);
			$this->view_data["links"] = NULL;
		}else{
			$this->load->helper('text');
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			/* pagenation */
			$this->load->library('pagination');
			$this->pagnate_config['base_url'] = base_url().'student_fees/assessment';
			$this->pagnate_config['total_rows'] = $this->M_fees->count_all_fees('assess');
			$this->pagnate_config['per_page'] = 10;
			$this->pagination->initialize($this->pagnate_config);
			
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$this->view_data['fee_data'] = $this->M_fees->fetch_fees('assess',$this->pagnate_config["per_page"], $page);
			$this->view_data["links"] = $this->pagination->create_links();
		}
	}
	
	function add()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['auth'] = $this->authlink-> generate_authlink();
		$this->form_validation->set_rules('fee_name','Name of Fee','required|trim');
		$this->form_validation->set_rules('fee_desc','Fee Description','trim');
		$this->form_validation->set_rules('fee_rate','Fee Rate','required|trim|numeric');
		$this->form_validation->set_rules('fee_type','Fee Type','required|trim');
		$this->form_validation->set_rules('fee_order','Fee Order','trim|numeric');
		
		if($this->input->post('add_fee'))
		{
			//$gradelevels = $this->input->post('grade_level');
			//foreach ($gradelevels as $selectedOption):
			//echo $selectedOption."\n";
			//endforeach;
			
			
			switch($type = strtolower($this->input->post('fee_type')))
			{
				case $type == 'is_misc_fee':
					$input['is_misc_fee'] = 1;
					$input['fee_stat'] = 'active';
					$this->form_validation->set_rules('grade_level','Grade Level','required');
					//$input['grade_level'] = $this->input->post('grade_level');
					$gradelevels = $this->input->post('grade_level');
					$typer = 'miscellaneous';
				break;
				case $type == 'is_elective_fee':
					$input['is_elective_fee'] = 1;
					$input['fee_stat'] = 'active';
					$typer = 'electives';
				break;
				case $type == 'is_tuition_fee':
					$this->form_validation->set_rules('grade_level','Grade Level','required');
					$input['is_tuition_fee'] = 1;
					$input['fee_stat'] = 'active';
					//$input['grade_level'] = $this->input->post('grade_level');
					$gradelevels = $this->input->post('grade_level');
					$typer = 'tuition';
				break;
				case $type == 'is_other_fee':
					$input['is_other_fee'] = 1;
					$input['fee_stat'] = 'active';
					$typer = 'others';
				break;
				case $type == 'is_cca_fee':
					$this->form_validation->set_rules('grade_level','Grade Level','required');
					$input['is_cca_fee'] = 1;
					$input['fee_stat'] = 'active';
					//$input['grade_level'] = $this->input->post('grade_level');
					$gradelevels = $this->input->post('grade_level');
					$typer = 'cca';
				break;
				case $type == 'is_uniform_fee':
					$input['is_uniform_fee'] = 1;
					$input['fee_stat'] = 'active';
					$typer = 'uniform';
				break;
				case $type == 'is_assess_fee':
					$input['is_assess_fee'] = 1;
					$input['fee_stat'] = 'active';
					$typer = 'assessment';
				break;
			}

			
			$input['fee_name'] = $this->input->post('fee_name');
			$input['fee_desc'] = $this->input->post('fee_desc');
			$input['fee_rate'] = str_replace(array(' ',','),'',$this->input->post('fee_rate'));
			$input['fee_order'] = $this->input->post('fee_order');
			$input['ay_id'] = $this->current_school_year;
			
			$fee_new = $this->input->post('fee_new');
			$fee_old = $this->input->post('fee_old');
			
			if($fee_new != '' && $fee_old != '')
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = 'yes';
			}
			elseif($fee_new != '' && $fee_old == '')
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = '';
			}
			elseif($fee_new == '' && $fee_old != '')
			{
				$input['fee_new'] = '';
				$input['fee_old'] = 'yes';
			}
			else
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = 'yes';
			}
			
			if($this->form_validation->run() == FALSE)
			{

			}else{
				if($gradelevels){
				$messages = '';
				foreach ($gradelevels as $selectedOption):
					$input['grade_level'] = $selectedOption;
					$result = $this->M_fees->add_fees($input)['status'];
				if($result == 'true'){
					$messages .= 'Fee '.$this->input->post('fee_name').' successfully added. ';
				}elseif($result == 'exist'){
					$messages .= 'Fee '.$this->input->post('fee_name').' cannot be added. Fee already exists. ';
				}elseif($result == 'false'){
					$messages .= 'Error occured while adding '.$this->input->post('fee_name').' to fees. Please try again. ';
				}
				endforeach;
					$this->_msg('n',$messages,'student_fees/'.$typer);
				}else{
				$result = $this->M_fees->add_fees($input)['status'];
				if($result == 'true'){
					$this->_msg('s','Fee '.$this->input->post('fee_name').' successfully added.','student_fees/'.$typer);
				}elseif($result == 'exist'){
					$this->_msg('n','Fee '.$this->input->post('fee_name').' cannot be added. Fee already exists.','student_fees/'.$typer);
				}elseif($result == 'false'){
					$this->_msg('e','Error occured while adding '.$this->input->post('fee_name').' to fees. Please try again.','student_fees/'.$typer);
				}
				}
			}
			
		}
		
	}
	
	function update_fees($id = '')
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['auth'] = $this->authlink-> generate_authlink();
		$this->form_validation->set_rules('fee_name','Name of Fee','required|trim');
		$this->form_validation->set_rules('fee_desc','Fee Description','trim');
		$this->form_validation->set_rules('fee_rate','Fee Rate','required|trim|numeric');
		$this->form_validation->set_rules('fee_type','Fee Type','required|trim');
		$this->form_validation->set_rules('fee_order','Fee Order','trim|numeric');

		if($id == '' OR !(int)$id)
		{

			if($this->input->post('update_fees'))
			{
				$fee_id = $this->input->post('fee_id');
				$input['fee_name'] = $this->input->post('fee_name');
				$input['fee_rate'] = $this->input->post('fee_rate');
				$input['fee_desc'] = $this->input->post('fee_desc');
				$input['fee_new'] = $this->input->post('fee_new');
				$input['fee_old'] = $this->input->post('fee_old');
				$input['fee_order'] = $this->input->post('fee_order');
				$input['grade_level'] = $this->input->post('grade_level');
				$input['fee_stat'] = $this->input->post('fee_stat');
				
			$fee_new = $this->input->post('fee_new');
			$fee_old = $this->input->post('fee_old');
			
			if($fee_new != '' && $fee_old != '')
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = 'yes';
			}
			elseif($fee_new != '' && $fee_old == '')
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = '';
			}
			elseif($fee_new == '' && $fee_old != '')
			{
				$input['fee_new'] = '';
				$input['fee_old'] = 'yes';
			}
			else
			{
				$input['fee_new'] = 'yes';
				$input['fee_old'] = 'yes';
			}
				
			switch($this->input->post('fee_type'))
			{
				case 'is_other_fee':
					$input['is_other_fee'] = 1;
					$input['is_tuition_fee'] = 0;
					$input['is_cca_fee'] = 0;
					$input['is_misc_fee'] = 0;
					$input['is_assess_fee'] = 0;
					break;
				case 'is_tuition_fee':
					$input['is_other_fee'] = 0;
					$input['is_tuition_fee'] = 1;
					$input['is_cca_fee'] = 0;
					$input['is_misc_fee'] = 0;
					$input['is_assess_fee'] = 0;
					break;
				case 'is_cca_fee':
					$input['is_other_fee'] = 0;
					$input['is_tuition_fee'] = 0;
					$input['is_cca_fee'] = 1;
					$input['is_misc_fee'] = 0;
					$input['is_assess_fee'] = 0;
					break;
				case 'is_misc_fee':
					$input['is_other_fee'] = 0;
					$input['is_tuition_fee'] = 0;
					$input['is_cca_fee'] = 0;
					$input['is_misc_fee'] = 1;
					$input['is_assess_fee'] = 0;
					break;
				case 'is_assess_fee':
					$input['is_other_fee'] = 0;
					$input['is_tuition_fee'] = 0;
					$input['is_cca_fee'] = 0;
					$input['is_misc_fee'] = 0;
					$input['is_assess_fee'] = 1;
					break;
				default:
					break;
			}
				
				//vd($input);
				
				$fee_data = $this->M_fees->get_all_fees($fee_id);
				if($this->form_validation->run() !== FALSE)
				{
					switch($result = $this->M_fees->update_fees($input,$fee_id)['status'])
					{
						case $result == 'true':
							$this->_msg('s','Fee was updated successfully.','update-fees/'.$fee_id);
						break;
						case $result == 'exist':
							$this->_msg('n','Fee already exists.','update-fees/'.$fee_id);
						break;
						case $result == 'false':
							$this->_msg('e','Fee was not updated, no data was changed.','update-fees/'.$fee_id);
						break;
					}
				}else{
					$this->view_data['fee_data'] = $fee_data;
				}
			}else{
				redirect();
			}
		}else{
			$this->view_data['fee_data'] = $this->M_fees->get_all_fees($id);
		}
	}
	
	public function enable_misc_fee($id = FALSE,$auth = FALSE)
	{
		if(is_numeric($id) AND $this->authlink->check_hash_make($id,$auth))
		{	
			if($this->M_fees->enable_misc_fee($id))
			{
				$this->_msg('s','Fee was successfully enabled','student_fees/miscellaneous');
			}else{
				$this->_msg('s','An error occured while the fee was being enabled. Please try again.',$last_link);
			}
			
		}elseif($this->authlink->quick_check_hash_make($id,$auth) == FALSE){
			show_error('Token Is Expired,Go back to last page and refresh the page. And Try The link again.',404);
		}else{
			show_error('Error while processing, Insufficient data was supplied. Please Go back to the last page and refresh, Then try the link again.',404);
		}
	}
	
	public function enable_tuit_fee($id = FALSE,$auth = FALSE)
	{
		if(is_numeric($id) AND $this->authlink->check_hash_make($id,$auth))
		{
			if($this->M_fees->enable_tuit_fee($id))
			{
				$this->_msg('s','Fee was successfully Enabled','student_fees/tuition');
			}else{
				$this->_msg('s','An error occured while the fee was being enabled. Please try again.','student_fees/fees#tuition');
			}
			
		}elseif($this->authlink->quick_check_hash_make($id,$auth) == FALSE){
			show_error('Token Is Expired,Go back to last page and refresh the page. And Try The link again.',404);
		}else{
			show_error('Error while processing, Insufficient data was supplied. Please Go back to the last page and refresh, Then try the link again.',404);
		}
	}
	
	public function enable_cca_fee($id = FALSE,$auth = FALSE)
	{
		if(is_numeric($id) AND $this->authlink->check_hash_make($id,$auth))
		{
			if($this->M_fees->enable_cca_fee($id))
			{
				$this->_msg('s','Fee was successfully enabled','student_fees/cca');
			}else{
				$this->_msg('s','An error occured while the fee was being enabled. Please try again.','student_fees/fees#cca');
			}
			
		}elseif($this->authlink->check_hash_make($id,$auth) == FALSE){
			show_error('Expired Toekn. Go back to last page and refresh the page. Try the link again.',404);
		}else{
			show_error('Error while processing. Insufficient data was supplied. Please Go back to the last page and refresh. Try the link again.',404);
		}
	}
	
	public function disable_fee($id = FALSE,$auth = FALSE)
	{
		if(is_numeric($id) AND $this->authlink->check_hash_make($id,$auth))
		{
			if($this->M_fees->disable_fee($id))
			{
				$this->_msg('s','Fee was successfully disabled','student_fees/fees');
			}else{
				$this->_msg('s','An error occured while the fee was being disabled. Please try again.','student_fees/fees');
			}
			
		}elseif($this->authlink->check_hash_make($id,$auth) == FALSE){
			show_error('Expired Token. Go back to last page and refresh the page. Try the link again.',404);
		}else{
			show_error('Error while processing. Insufficient data was supplied. Please go back to the last page and refresh. Try the link again.',404);
		}
	}
	
	public function delete_fee($raw)
	{
		$x = $this->hs->decrypt($raw);
		$uri = $this->uri->segment(2);
		if($x AND $uri)
		{

			$fee_id = $x[0];
			
			if($this->M_fees->delete_fee($fee_id))
			{
				$this->_msg('s','Fee was successfully deleted.','student_fees/fees#'.$uri);
			}else{
				$this->_msg('n','Unable to delete fees.','student_fees/fees');
			}
		}else{
			show_404();
		}
	
	
	}
	
	public function payment_modes()
	{
		$this->load->library('form_validation');
		$this->load->model('M_payment_mode','mpm');
		$this->load->model('M_payment_mode_value','mpmv');
		$this->load->model('M_levels','l');
		$this->load->model('M_fees');

		$levels = $this->l->get_all_levels();
		
		$this->form_validation->set_rules('plan_name', 'Payment Plan', 'trim|required|is_unique[payment_modes.payment_mode_name]');
		
		if($this->input->post('add_payment_plan')):

			if ($this->form_validation->run() !== FALSE):
				$data['payment_mode_name'] = strtolower($this->input->post('plan_name'));
				if($this->mpm->insert_payment_modes($data)):
					$this->_msg('s','Successfully saved Payment Plan','student_fees/payment_modes');
				else:
					$this->view_data['system_message'] = $this->_msg('e','An Error occurred while saving data. Please try again!','student_fees/payment_modes');
				endif;
			endif;
			
		endif;
		
		if($this->input->post('set_payment_plan')):
			foreach($levels as $level):
				foreach($payment_plans as $payment_plan):
					if($this->mpmv->get_payment_plan_values($level->level_id,$payment_plan['id']) === TRUE):
						$data['payment_plan_amount'] = $this->input->post($level->level_id.'_'.$payment_plan['id']);
						$this->db->where('level_id', $level->level_id);
						$this->db->where('payment_mode_id', $payment_plan['id']);
						$this->db->update('payment_mode_values', $data); 
					else:
						$data['level_id'] = $level->level_id;
						$data['payment_mode_id'] = $payment_plan['id'];
						$data['payment_plan_amount'] = $this->input->post($level->level_id.'_'.$payment_plan['id']);
						$this->db->insert('payment_mode_values', $data); 
					endif;
				endforeach;
			endforeach;
							if($this->db->affected_rows() >= 1):
								$this->_msg('s','Successfully saved Payment Plan Value.','student_fees/payment_modes');
							endif;
		endif;

		if($this->input->post('set_payment_mode_another')){
			foreach($levels as $level):
					if($this->mpmv->get_payment_plan_values($level->level_id,$this->input->post('pm_id')) === TRUE):
						$data['payment_plan_tuit'] = str_replace(',', '', $this->input->post('payment_plan_tuit_'.$level->level_id));
						$data['payment_plan_matr'] =  str_replace(',', '', $this->input->post('payment_plan_matr_'.$level->level_id));
						$data['payment_plan_amount'] = str_replace(',', '', $this->input->post('payment_plan_tuit_'.$level->level_id)) + str_replace(',', '', $this->input->post('payment_plan_matr_'.$level->level_id));
						$this->db->where('level_id', $level->level_id);
						$this->db->where('payment_mode_id', $this->input->post('pm_id'));
						$this->db->where('ay_id', $this->current_school_year);
						$this->db->update('payment_mode_values', $data); 
					else:
						$data['level_id'] = $level->level_id;
						$data['ay_id'] = $this->current_school_year;
						$data['payment_mode_id'] = $this->input->post('pm_id');
						$data['payment_plan_tuit'] = str_replace(',', '', $this->input->post('payment_plan_tuit_'.$level->level_id));
						$data['payment_plan_matr'] =  str_replace(',', '', $this->input->post('payment_plan_matr_'.$level->level_id));
						$data['payment_plan_amount'] = str_replace(',', '', $this->input->post('payment_plan_tuit_'.$level->level_id)) + str_replace(',', '', $this->input->post('payment_plan_matr_'.$level->level_id));
						$this->db->insert('payment_mode_values', $data);
					endif;
			endforeach;
				if($this->db->affected_rows() >= 1):
					$this->_msg('s','Successfully saved Payment Plan Value.','student_fees/payment_modes');
				endif;
		}
		
		//$this->view_data['payment_plans'] = $this->mpm->get_payment_plans();
		//$this->view_data['levels'] = $this->l->get_all_levels();
	/*
		$this->load->model('M_payment_group','mpg');
		$glevel = $this->mpg->show_payment_group_fees();
		
		if($glevel == TRUE)
		{
			$levels = array_map('current',(array)$glevel);
			$levels = array_map('strtolower',$levels);
			$levels = array_map(function($x){
				return preg_replace('~[^a-zA-Z0-9]+~', '', trim(strtolower($x)));
			},$levels);
			
			$this->view_data['available_levels'] = $levels;
			
			foreach($glevel as $k => $v)
			{
				$var = preg_replace('~[^a-zA-Z0-9]+~', '', trim(strtolower($v['group_name']))).'_data';
				$this->view_data[$var]['fee_mode'] = $this->M_fees->payment_modes($v['id']);
				$this->view_data[$var]['fee_total']= $this->mpg->total_fee_for_group($v['id']);
				$this->view_data[$var]['other']= $this->mpg->get_group($v['id']);
			}
		}
		
		$this->view_data['form_token'] = $this->token->get_token();
		
		if($this->input->post('payment_mode'))
		{
			$deep_link = $this->input->post('id');
			
			
			
			// pd($this->input->post());
			if($this->token->validate_token($this->input->post('spl_loc')))
			{
				$post = $this->input->post();
				unset($post['payment_mode']);
				unset($post['id']);
				if($this->M_fees->update_payment_modes($post,$this->input->post('type'),$levels))
				{
					$this->token->destroy_token();
					$this->_msg('s','Successfully Updated Payment Mode','student_fees/payment_modes#'.$deep_link);
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An Error occured while updating data, please try again','student_fees/payment_modes#'.$deep_link);
				}
			}else{
				$this->token->destroy_token();
				$this->_msg('e','Invalid Form token supplied, Please try again','student_fees/payment_modes#'.$deep_link);
			}
		}else{
			
		}
	*/
		$this->view_data['fee_data'] = $this->M_fees->get_tuition_fees();
		$this->view_data['payment_plans'] = $payment_plans = $this->mpm->get_payment_plans();
		$this->view_data['levels'] = $this->l->get_all_levels();
		$this->view_data['payment_plan_values'] = $this->mpmv->get_payment_plan_values();

		$this->view_data['form_token'] = $this->token->get_token();
	}
	
	public function edit_payment_mode($id = FALSE)
	{
		$this->load->library('form_validation');
		$this->load->model('M_payment_mode','mpm');
		
		$this->view_data['payment_plan'] = $this->mpm->get_payment_plans($id);
		
		$this->form_validation->set_rules('plan_name', 'Payment Plan', 'trim|required|is_unique[payment_modes.payment_mode_name]');
		if($id !== FALSE){
		if($this->input->post('update_payment_plan')):

			if ($this->form_validation->run() !== FALSE):
				$data['payment_mode_name'] = strtolower($this->input->post('plan_name'));
				if($this->mpm->update_payment_modes($id,$data)):
					$this->_msg('s','Successfully updated Payment Plan.','student_fees/payment_modes');
				else:
					$this->view_data['system_message'] = $this->_msg('e','An Error occured while saving data. Please try again.','student_fees/payment_modes');
				endif;
			endif;
			
		endif;
		}else{
			$this->_msg('e','Record no Found!','student_fees/payment_modes');
		}
	}
	
	public function delete_payment_mode($id = FALSE)
	{
		$this->load->model('M_payment_mode','mpm');
		if($id !== FALSE){
	
			if($this->mpm->delete_payment_modes($id)):
				$this->_msg('s','Successfully deleted Payment Plan.','student_fees/payment_modes');
			else:
				$this->view_data['system_message'] = $this->_msg('e','An Error occured while deleting data. Please try again.','student_fees/payment_modes');
			endif;
				
		}else{
			$this->_msg('e','Record no Found!','student_fees/payment_modes');
		}
	}
	
	public function fees()
	{
		$this->view_data['auth'] = $this->authlink-> generate_authlink();
		$this->view_data['tuit_fee'] = $this->M_fees->get_fees('tuit');
		$this->view_data['misc_fee'] = $this->M_fees->get_fees('misc');
		$this->view_data['other_fee'] = $this->M_fees->get_fees('other');
		$this->view_data['cca_fee'] = $this->M_fees->get_fees('cca');
		$this->view_data['elec_fee'] = $this->M_fees->get_fees('elec');
		$this->view_data['assess_fee'] = $this->M_fees->get_fees('assess');
	}
	
	/* =====================================START SCHOLARSHIPS===================================================== */	
	function scholarships()
	{
		$this->load->library(array('grocery_CRUD'));
		try
		{
			$crud = new grocery_CRUD();
			$crud->set_theme('datatables'); // theme datatables or flexigrid
			$crud->set_table('student_scholarships'); // set table
			$crud->set_subject('Student Scholarships');
			$crud->display_as('ss_name','Scholarship Name')
				 ->display_as('ss_desc','Description');
			$crud->required_fields('ss_name','ss_desc');
			$crud->columns('ss_name','ss_desc','ss_tuit_percentage','ss_cca_percentage','ss_misc_percentage','ss_tuit_cash','ss_cca_cash','ss_misc_cash');
			$crud->fields('ss_updated','ss_created','ss_name','ss_desc');
			$crud->change_field_type('ss_updated','invisible');
			$crud->change_field_type('ss_created','invisible');
			$crud->edit_fields('ss_name','ss_desc','ss_created','ss_tuit_percentage','ss_cca_percentage','ss_misc_percentage','ss_tuit_cash','ss_cca_cash','ss_misc_cash');
			$crud->add_fields('ss_name','ss_desc','ss_created','ss_tuit_percentage','ss_cca_percentage','ss_misc_percentage','ss_tuit_cash','ss_cca_cash','ss_misc_cash');
			//display add and edit name
			$crud->display_as('ss_name','Scholarship Name')
				 ->display_as('ss_desc','Description')
				 ->display_as('ss_tuit_percentage','Tuition Deduction in Percentage')
				 ->display_as('ss_cca_percentage','Matriculation Deduction in Percentage')
				 ->display_as('ss_misc_percentage','Miscellaneous Deduction in Percentage')
				 ->display_as('ss_tuit_cash','Tuition Deduction (cash)')
				 ->display_as('ss_cca_cash','Matriculation Deduction (cash)')
				 ->display_as('ss_misc_cash','Miscellaneous Deduction (cash)');
			// validation errors 	 
			$crud->set_rules('ss_name','Scholarship Name','required|trim|htmlspecialchars')
				 ->set_rules('ss_desc','Description','required|trim|htmlspecialchars')
				 ->set_rules('ss_tuit_percentage','Tuition Deduction in Percentage','numeric|trim|htmlspecialchars')
				 ->set_rules('ss_cca_percentage','Matriculation Deduction in Percentage','numeric|trim|htmlspecialchars')
				 ->set_rules('ss_misc_percentage','Miscellaneous Deduction in Percentage','numeric|trim|htmlspecialchars')
				 ->set_rules('ss_tuit_cash','Tuition Deduction (cash)','numeric|trim|htmlspecialchars')
				 ->set_rules('ss_cca_cash','Matriculation Deduction (cash)','numeric|trim|htmlspecialchars')
				 ->set_rules('ss_misc_cash','Miscellaneous Deduction (cash)','numeric|trim|htmlspecialchars');
			//remove certain buttons
			$crud->unset_export()
			     ->unset_print()
				 ->unset_texteditor('ss_desc');
			//callbacks
			$crud->callback_before_insert(array($this,'before_insert'));
			$crud->callback_before_update(array($this,'before_update'));
			//generate and send output to view
			$output = $crud->render(); // render
			$this->view_data['output'] = $output; // pass to views as data
		}catch(Exception $e)
		{
			$this->view_data['system_message']='<div class="error">'.$e.'</div>';
		}
	}
	
	function before_insert($data_as_array)
	{
		$data_as_array['ss_created'] = NOW;
		return $data_as_array;
	}

	function before_update($data_as_array)
	{
		$data_as_array['ss_updated'] = NOW;
		return $data_as_array;
	}
	
/* ===============================================grade level fee=============================================== */
	
	function grade_level_fees()
	{
		$this->load->helper('misc');
		$this->view_data['level_fees'] = function($x){return $this->M_level_fees->get_mrt_fees_for_level($x);};
		$this->view_data['level_data'] = $this->M_level_fees->get_grade_level();
	}
	
	function set_grade_level_fees($id='')
	{
		$this->load->helper('misc');
		$this->view_data['all_fees_assigned'] =$this->M_level_fees->get_mrt_fees_for_level($id,FALSE);
		$this->view_data['system_message'] = $this->_msg();
		
		if($id =='' OR !(int)$id)
		{
			
			if($this->input->post('set_grade_level_fees') !== FALSE)
			{
				$level_id = $this->input->post('lf_level_id');
				$fees_post= $this->input->post('fee');
				if(!empty($fees_post))
				{
					foreach($fees_post as $key => $value)
					{
						$input[$key]['lf_fee_id'] = $value['lf_fee_id'];
						$input[$key]['lf_level_id'] = $level_id ;
					}
				}else{
					$input = 0;
				}
				
				switch($result = $this->M_level_fees->update_grade_level_fees($input,$level_id)['status'])
				{
					case $result == 'true':
						$this->_msg('s','Your Changes was saved','student_fees/set_grade_level_fees/'.$level_id);
					break;
					case $result == 'false':
						$this->_msg('e','An error Occured while processing request','student_fees/set_grade_level_fees/'.$level_id);
					break;
					case $result == 'nodelete':
						$this->_msg('e','Unable to delete data','student_fees/set_grade_level_fees/'.$level_id);
					break;
				}
				
			$this->load->helper('foreach_helper');
			
			$all_fees = all_fees($this->M_level_fees->get_all_fees());
			$sel_fees = selected_fees($this->M_level_fees->get_sel_fees($level_id));
			$avl_fees = available_fees($all_fees,$sel_fees);
			$this->view_data['available_fees'] = $avl_fees;
			$this->view_data['selected_fees'] = $sel_fees;
			$this->view_data['level_data'] = $this->M_level_fees->get_grade_level($level_id);				
				
				
			}else{
				redirect('admin/grade_level_fees');
			}
		}else
		{
			$this->load->helper('foreach_helper');
			
			$all_fees = all_fees($this->M_level_fees->get_all_fees());
			$sel_fees = selected_fees($this->M_level_fees->get_sel_fees($id));
			$avl_fees = available_fees($all_fees,$sel_fees);
			$this->view_data['available_fees'] = $avl_fees;
			$this->view_data['selected_fees'] = $sel_fees;
			$this->view_data['level_data'] = $this->M_level_fees->get_grade_level($id);
		}
	}
	
	public function student_charges()
	{
		
	
	
	
	}
	
	public function add_discount_label()
	{
		$this->load->model(array('M_discount_labels'));
		$this->load->library('form_validation');
		
		if($this->input->post('add_label') !== FALSE)
		{
			
			$this->form_validation->set_rules('label_name', 'Discount Label', 'trim|required|is_unique[discount_labels.label]');
			if ($this->form_validation->run())
			{
				$data['label'] = $this->input->post('label_name');
				if($this->M_discount_labels->add_discount_label($data))
				{
					$this->_msg('s','Discount label saved.','student_fees/discount_labels');
				}else{
					$this->_msg('e','An error occured while processing request. Please try again.','student_fees/add_discount_label');
				}
			}
		}
	}
	
	public function discount_labels()
	{
		$this->load->model(array('M_discount_labels'));
		
		$this->view_data['discounts'] = $this->M_discount_labels->get_labels();
	}
	
	public function update_discount_label($id=FALSE)
	{
		$this->load->model(array('M_discount_labels'));
		$this->load->library('form_validation');
		
		if($id)
		{
			$this->view_data['discount'] = $this->M_discount_labels->get_labels($id);
			if($this->input->post('update_label') !== FALSE)
			{
				$this->form_validation->set_rules('label_name', 'Discount Label', 'trim|required|is_unique[discount_labels.label]');
				if ($this->form_validation->run())
				{
					$id = $this->input->post('label_id');
					$data['label'] = $this->input->post('label_name');
					if($this->M_discount_labels->update_discount_label($id,$data))
					{
						$this->_msg('s','Discount label saved.','student_fees/discount_labels');
					}else{
						$this->_msg('e','An error occured while processing request. Please try again.','student_fees/add_discount_label');
					}
				}
			}
		}else{
			show_404();
		}
	}
	
	public function delete_discount_label($raw)
	{
		$this->load->model(array('M_discount_labels'));
		$x = $this->hs->decrypt($raw);
		
		if($x)
		{
			$label_id = $x[0];
			
			if($this->M_discount_labels->delete_discount_label($label_id))
			{
				$this->_msg('s','Label was successfully deleted.','student_fees/discount_labels');
			}else{
				$this->_msg('n','Unable to delete discount label.','student_fees/discount_labels');
			}
		}else{
			show_404();
		}
	}
}